#ifndef GAMESETTINGS_H
#define GAMESETTINGS_H

#include <QWidget>
#include <QString>
#include <QTcpServer>
#include <QTcpSocket>
#include <QThread>
#include "gameconstants.h"

class ConnectThread;

namespace Ui {
class GameSettings;
}

class GameSettings : public QWidget
{
    Q_OBJECT

private:

    PlayerType player;  // The player that hosts the game will always be on the left side and have Id 0,
                        // and the one that connects will be on the right side with Id 1
    QString player1Name, player2Name;

    QTcpServer server;
    QTcpSocket* socket;
    ConnectThread* thread;

    GameMode gameMode;
    bool connected;
    bool rightPlayerIsReady;

    int duration;

public:

    GameSettings(QWidget *parent = 0);
    ~GameSettings();

    Ui::GameSettings* getUi() {return ui;}

    void updateWidgetState();   // enables/disables all the widgets according to the current settings
    void addToLog(QString msg, QString senderName);

    // getters
    PlayerType getPlayer() { return player; }
    bool getConnected() { return connected; }
    GameMode getGameMode() { return gameMode; }
    int getDuration() { return duration; }
    QString getPlayer1Name() { return player1Name; }
    QString getPlayer2Name() { return player2Name; }

    void setRightPlayerIsReady(bool b) { rightPlayerIsReady = b; }

private slots:
    void on_radBtnSing_clicked();
    void on_radBtnLoc_clicked();
    void on_radBtnNet_clicked();

    void on_btnConnectServer_clicked();
    void on_btnCreateServer_clicked();
    void on_btnStart_clicked();

    void on_lineEnterChat_returnPressed();
    void on_lnIpAddress_returnPressed();
    void on_lineName_returnPressed();
    void on_lineName_editingFinished();
    void on_lineName2_editingFinished();    // for local multiplayer
    void on_lstDuration_currentIndexChanged(int index);
    void on_lstDifficulty_currentIndexChanged(int index);


    // server methods
    void _server_clientConnected();
    void _server_dataReceived();
    void _server_clientDisconnected();

    // client methods
    void _client_dataReceived();
    void _client_disconnectedFromServer();
    void _client_connectFinished();

public slots:
    void _server_sendToClient(QString msg);
    void _client_sendToServer(QString msg);

signals:
    void updateWindowState(bool m, bool g, bool s, bool h);
    void gameStarted();

    // signals to tell network stuff to gamewindow
    void toGameWindow_connectionLost();
    void toGameWindow_ClientReceivedData(QString data);
    void toGameWindow_ServerReceivedData(QString data);

private:
    Ui::GameSettings *ui;

};


class ConnectThread : public QThread {
private:
    QString hostname;
    bool success;
    QString errorMsg;

    QTcpSocket *socket;
public:
    ConnectThread(QString _hostname) : hostname(_hostname) { }

    bool getSuccess() { return success; }
    QString getErrorMsg() { return errorMsg; }
    QTcpSocket* getSocket() { return socket; }

    void run();
};

#endif // GAMESETTINGS_H
